<?php

/*
 * @author Ebrain Cedeno <ebraincedeno@gmail.com>
 */

abstract class Controller extends Templete{
    
    
    public function render($class_name,$view){
        
        //cargando el templete
        
        $templete = new Templete();
        $templete->loadThemeContent();
                 
        if (file_exists('view'.DIRECTORY_SEPARATOR.strtolower($class_name).DIRECTORY_SEPARATOR.strtolower($view).".php")){
            
            $ObjectToArray = (array)$this;
            extract($ObjectToArray);
            
            $valor_vista = "include '".'view'.DIRECTORY_SEPARATOR.strtolower($class_name).DIRECTORY_SEPARATOR.strtolower($view).".php';";
            $temporal_name = "myTemplete";
            
            $html_temp = self::$headerTheme;
            $html_temp .= '<?php '.$valor_vista.'?>';
            $html_temp .= self::$footerTheme;
            if (file_exists(PATH_ROOT."/temp/templete/".$temporal_name.".php")){
                unlink(PATH_ROOT."/temp/templete/".$temporal_name.".php");
            }
            if (!file_put_contents(PATH_ROOT."/temp/templete/".$temporal_name.".php", $html_temp)){
                throw new Exception('Error al guardar el temporalde templete');
            }
            else{
                
                include PATH_ROOT."/temp/templete/".$temporal_name.".php";
            }
            
        }
        
    }
    
    public function renderPartial($class_name,$view,$object=null,$return=false){
        
        if (file_exists('view'.DIRECTORY_SEPARATOR.strtolower($class_name).DIRECTORY_SEPARATOR.strtolower($view).".php")){
            
            $ObjectToArray = (array)$object;
            extract($ObjectToArray);
            $valor_vista = "include '".'view'.DIRECTORY_SEPARATOR.strtolower($class_name).DIRECTORY_SEPARATOR.strtolower($view).".php';";
            if ($return == false){
                echo eval($valor_vista);
            }
            elseif ($return == true){
                return $valor_vista;
            }
        }
        else{
            throw new Exception("No se encontro la vista solicitada");
        }
    }
    
    public function redirec($url,$time=0,$param=array(),$optionsHtml=array()){
        
        if (count($param) > 0){
            $parameters = '?';
            foreach ($param as $key=>$value){
                $parameters .= $key."=".$value."&";
            }
            $parameters = substr($parameters, 0,-1);
        }
        else{
            $parameters = '';
        }
        
        header("refresh:".$time.";url=".$url.$parameters);
        
    }
    
}
?>
